// 虚析构和纯虚析构
// 虚析构为了解决通过父类指针释放子类对象（子类在堆区开辟的空间）
// 若子类中没有堆区分配内存，可不写虚析构
#include <iostream>
#include <string>
using namespace std;
class Animal {
public:
    Animal();

    // 析构函数前加virtual代表虚析构（可解决子类堆分配未释放问题）
    // virtual ~Animal() {
    //     cout << "Animal construct" << endl;
    // }

    // 纯虚析构函数
    virtual ~Animal() = 0;
    // 含有纯虚析构函数就是抽象类
    // 纯虚析构类内声明，类外初始化

    // 纯虚函数
    virtual void speak() = 0;
};
Animal::Animal() {
    cout << "Animal construct" << endl;
}
Animal::~Animal() {
    cout << "Animal destruct" << endl;
}
class Cat: public Animal {
public:
    string *m_name;
    Cat();
    Cat(string name);
    ~Cat();
    void speak() {
        cout << "猫说喵喵喵" << endl;
    }
};
Cat::Cat() {}
Cat::Cat(string name) {
    m_name = new string(name);
    cout << *this->m_name << "cat construct" << endl;
}
Cat::~Cat() {
    cout << *this->m_name << " cat destruct" << endl;
    if (this->m_name != nullptr) {
        delete this->m_name;
        this->m_name = nullptr;
    }
    
}
void test1() {
    Animal *animal = new Cat("Tom");
    animal->speak();
    delete animal;
}
int main(void) {
    test1();

    return 0;
}